package grandPrize

import (
	"context"
	"math"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	"gitee.com/liuxuezhan/ar-platform/admin-api/pkg/code"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/grandprize"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/rpc"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/userServiceClient/userrpc"
	"github.com/zeromicro/go-zero/core/logc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GrandPrizeListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 列表
func NewGrandPrizeListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GrandPrizeListLogic {
	return &GrandPrizeListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GrandPrizeListLogic) GrandPrizeList(req *types.GrandPrizeListReq) (resp *types.GrandPrizeListDataRes, err error) {
	resData := make([]types.GrandPrizeListItemRes, 0)

	resp = &types.GrandPrizeListDataRes{
		List: resData,
		Pagination: types.Pagination{
			PageSize:  int32(req.Size),
			Page:      int32(req.Page),
			Total:     0,
			TotalPage: 0,
		},
	}

	u, err := l.svcCtx.SettingService.GrandPrize.GrandPrizeList(l.ctx, &grandprize.GrandPrizeListReq{Page: req.Page, Size: req.Size, Status: rpc.StatusType(req.Status), MerchantId: 1})

	if err != nil {
		logc.Error(l.ctx, "GrandPrizeList err:", err)
		return nil, err
	}

	if u.Code != int64(code.SettingRpcSuccessful.Code()) {
		return nil, code.BusinessFailure
	}

	userIdMap := map[int64]int64{}
	for _, v := range u.Data.List {
		userIdMap[v.OperatorId] = v.OperatorId

		resData = append(resData, types.GrandPrizeListItemRes{
			Id:               v.Id,
			MerchantId:       v.MerchantId,
			MultiplierStart:  float64(v.MultiplierStart),
			MultiplierEnd:    float64(v.MultiplierEnd),
			OrderAmountStart: float64(v.OrderAmountStart),
			OrderAmountEnd:   float64(v.OrderAmountEnd),
			Bonus:            float64(v.Bonus),
			Sort:             v.Sort,
			SendDay:          v.SendDay,
			Status:           int64(v.Status),
			Operator:         v.Operator,
			OperatorId:       v.OperatorId,
			CreatedAt:        v.CreatedAt,
			UpdatedAt:        v.UpdatedAt,
		})
	}

	if len(userIdMap) > 0 {
		userIdList := make([]int64, 0)
		for _, v := range userIdMap {
			userIdList = append(userIdList, v)
		}

		infoResp, err := l.svcCtx.UserServiceRPC.AdminUserNameList(l.ctx, &userrpc.AdminUserNameListRequest{IdList: userIdList})
		if err != nil {
			logc.Error(l.ctx, "UserServiceRPC.AdminUserNameList err:", err)
		}

		if err == nil && len(infoResp.AdminUserNameList) > 0 {
			userResMap := map[int64]string{}
			for _, v := range infoResp.AdminUserNameList {
				userResMap[v.Id] = v.Nickname
			}

			if len(userResMap) > 0 {
				for index, v := range resData {
					if value, ok := userResMap[v.OperatorId]; ok {
						resData[index].Operator = value
					}
				}
			}
		}

	}

	resp.List = resData
	resp.Pagination.Total = u.Data.Count
	resp.Pagination.TotalPage = int64(math.Ceil(float64(resp.Pagination.Total) / float64(resp.Pagination.PageSize)))
	return resp, nil
}
